1283D - Christmas Trees - CodeForces Solution


graphs greedy shortest paths *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
///  order_of_key return number of elements less than x.
///  find_by_order return index.

const int N =  3*1e5 + 10;
const int M = 998244353;

#define ll long long int
#define ld long double
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ff first
#define ss second
#define rep1(i, n) for (ll i = 1; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define pb push_back
#define vi vector<int>
#define vll vector<long long>
#define vpll vector<pair<ll, ll>>
#define vvll vector<vector<ll>>
#define vvpll vector<vector<pll>>
#define pll pair<ll, ll>
const ll INF = LLONG_MAX;
ll binmult(int a, int b)
{
    ll ans = 0;
    while (b > 0)
    {
        if (b & 1)
            ans = (ans + a) % M;
        a = (a + a) % M;
        b >>= 1;
    }
    return ans;
}
ll binpow(int a, int b)
{
    ll ans = 1;
    while (b > 0)
    {
        if (b & 1)
            ans = binmult(ans, a);
        a = binmult(a, a);
        b >>= 1;
    }
    return ans;
}
ll bs_sqrt(ll x)
{
    ll left = 0, right = 2000000123;
    while (right > left)
    {
        ll mid = (left + right) / 2;
        if (mid * mid > x)
            right = mid;
        else
            left = mid + 1;
    }
    return left - 1;
}
ll inverse(ll x,ll M){
    ll y=binmult(x,M-2);
    return y;
}

// ll nCr(ll n, ll r)
// {
//     return ((fac(n)*inverse(fac(n-r)))%M*inverse(fac(r)))%M;
// }

//----------------------------------------------------------------------
// DSU
// vll par(N+1,0),sz(N+1,0);
// void make(int x){
//     par[x]=x;sz[x]=1;
// }
// int find(int x){
//     if(x==par[x]) return x;
//     return par[x]=find(par[x]);
// }
// void Union(int b,int a){
//     a=find(a),b=find(b);
//     // if(a!=b)
//     {
        
//         if(sz[a]<sz[b]) swap(a,b);
//         sz[a]+=sz[b];
//         par[b]=a;
//     }
// }
//--------------------------------------------------------------------
// Dynamic Range Minimum Segment Tree

// vll a(N), seg(4*N);
// void build(int ind, int low, int high){
//     if(low==high) {seg[ind]=a[low]; return ;}
//     ll mid=(high+low)/2;
//     build(2*ind+1, low, mid);
//     build(2*ind+2, mid+1,high);
//     seg[ind]=min(seg[2*ind+1], seg[2*ind+2]);
// }
// void update(ll ind, ll low,ll high, ll i, ll val){
//     if(low==high ){
//         if(low==i)
//         seg[ind]=val;
//         return ;
//     }
//     if(i<low || i>high) return ;
//     ll mid=(high+low)/2;
//     update(2*ind+1,low, mid, i, val);
//     update(2*ind+2,mid+1, high, i, val);
//     seg[ind]=min(seg[2*ind+1], seg[2*ind+2]);
//     return ;
// }
// ll query(ll ind, ll low,ll high, ll l, ll r){
//     if(l<=low && high<=r) return seg[ind];
//     if(high< l || low>r) return INT_MAX;
//     ll mid=(high+low)/2;
//     return min(query(2*ind+1, low, mid, l, r), query(2*ind+2, mid+1, high, l, r));
// }
//-----------------------------------------------------------------------
bool comp(pll &p, pll &q){
    if(p.ff== q.ff) return p.ss>q.ss;
    return p.ff<q.ff;
}
bool comp2(pll &p, pll &q){
    if(p.ff== q.ff) return p.ss>q.ss;
    return p.ff<q.ff;
}


// // vll dis(N,-INF), dis2()
// vpll dis(N), dis2(N);
// void dfs(ll i, vll &vis, vvll &g, vll &a){
//     vis[i]=1;
//     ll mx=0, mx2=0;
//     for(auto it: g[i]){
//         if(vis[i]) continue;
//         dfs(it,vis,g,a);
//         if(mx <= dis[it].ff) mx2=mx, ind2=ind1, ind1=it, mx=dis[it].ff;
//         else if(mx2 <= dis[it].ff ){

//         }
//     }
// }

void solve()
{
    ll n,m;cin>>n>>m;
    vll a(n);
    rep(i,n) cin>>a[i];
    sort(a.begin(), a.end());

    vll b=a;
    rep(i,n) b[i]*=-1;
    sort(b.begin(), b.end());
    
    queue<ll> q;
    map<ll,ll> mp;
    ll c=0;
    rep(i,n){
        q.push(a[i]);
        mp[a[i]]++;
    }
    // map<ll,ll> mp;
    vll v;
    while(!q.empty()){
        auto cur=q.front();
        q.pop();
        if(mp.find(cur-1)==mp.end()) {q.push(cur-1);mp[cur-1]++; v.pb(cur-1);}
        if(mp.find(cur+1)==mp.end()) {q.push(cur+1);mp[cur+1]++; v.pb(cur+1);}
        if(v.size()>=m) break;
    }
    // cout<<v.size()<<endl;
    ll d=0;
    rep(i,m){
        ll l=lower_bound(a.begin(), a.end(), v[i])-a.begin();
        ll u=lower_bound(b.begin(), b.end(), -v[i])-b.begin();

        ll mn=M;
        if(l!=a.size()) mn=abs(v[i]-a[l]);
        if(u!=b.size()) mn=min(mn,abs(v[i]+b[u]));
        // if(mn==M) cout<<i<<" "<<v[i]<<"\n";
        d+=mn;
    }
    cout<<d<<endl;
    rep(i,m) cout<<v[i]<<" ";
    cout<<endl;
}
    
//a[i]>=ur[i] a[i]<=mn[i]+ul[i] ur[i]=a[i]-ul[i] ul[i]=min(ul[i],a[i]-ur[i])

    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        int t=1;
        // cin >> t;
        ll tt = 1;
        ll p=1;
        while (t--)
        {    
            solve();
            tt++;
        }
        return 0;
    }
 
/*
to get the maxsum, and minsum of a array from start but processing from end:-
suppose s is the array, sul - suffix_minsum, sur - suffix_maxsum
for (int i = n - 1; i >= 0; --i){
            int d = s[i];
            sul.push_back(min(0, sul.back() + d));
            sur.push_back(max(0, sur.back() + d));
}
*/


Comments

Submit
0 Comments
More Questions

1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number